//
// Created by lymk on 2021/8/8.
//

#include "PhyComponent.h"
#include "../Actor/GameActor.h"
#include "../Actor/ItemActor.h"

ItemActor* PhyComponent::PreViewCollide(GMath::Rectangle box) {
    for(int i=0;i<collideBox.size();i++){
        auto relation = GMath::JudgeRelationAmongRectangles(box,collideBox[i]);
        if(relation == GMath::INTERSECT)
            return collideItem[i];
    }
    return nullptr;
}

void PhyComponent::update() {
    for(int i=0;i<collideItem.size();i++){
        if(collideItem[i]->getPicData() != nullptr)
            collideBox[i] = collideItem[i]->getCollideBox();
    }
}

void PhyComponent::AddCollideActor(ItemActor *actor) {
    collideBox.push_back(actor->getCollideBox());
    collideItem.push_back(actor);
}

void PhyComponent::DeleteCollideActor(ItemActor *actor) {
    int i = 0;
    for(i=0;i<collideBox.size();i++){
        if(collideItem[i] == actor){
            break;
        }
    }
    if(i==collideItem.size()-1){
        collideBox.pop_back();
        collideItem.pop_back();
    }else{
        for(int j=i+1;j<collideBox.size();j++){
            collideBox[j-1] = collideBox[j];
            collideItem[j-1] = collideItem[j];
        }
        collideBox.pop_back();
        collideItem.pop_back();
    }
}

void PhyComponent::AddCollideBox(GMath::Rectangle box) {
    collideBox.push_back(box);
    collideItem.push_back(new ItemActor());
}
